package vg.services.main_manager;

import tzhomestudio.framework.implementation.main_service.FrameworkMainService;
import vg.implementation.ll_services.data_base_service.SQLiteCacheGraphDataBaseService;
import vg.implementation.ll_services.lib_service.LibService;
import vg.interfaces.ll_services.data_base_service.IGraphDataBaseService;
import vg.interfaces.ll_services.lib_service.ILibService;
import vg.services.graph_layout_manager.GraphLayoutManager;
import vg.services.graph_layout_manager.interfaces.IGraphLayoutManager;
import vg.services.graph_view_manager.GraphViewManager;
import vg.services.graph_view_manager.interfaces.IGraphViewManager;
import vg.services.progress_manager.SimpleProgressManager;
import vg.services.progress_manager.interfaces.IProgressManager;
import vg.services.user_interface_manager.UserInterfaceManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * This class contains links to all services.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class VGMainService extends FrameworkMainService {
    // Core services
    private static ILibService libService;

    private static IGraphDataBaseService graphDataBaseManger;
	private static UserInterfaceManager userInterfaceManager;

	private static IProgressManager progressManager;
	private static ExecutorService threadPoolManager;

	private static IGraphLayoutManager graphLayoutManager;
	private static IGraphViewManager graphViewManager;

    // Mutex
    private static final Object generalMutex = new Object();

    /**
     * Executes initial preparations.
     */
    public static void executeInitialPreparations(String[] args) {
        synchronized (generalMutex) {
            try {
                FrameworkMainService.executeInitialPreparations();

                // add options
                cmdLine.addOption(VGMainServiceHelper.CmdLineOptions.appInstanceIdOption);

                cmdLine.parseCmdLineArgs(args);

                // execute preparations
                libService = new LibService();
                libService.loadLibs();

                graphDataBaseManger = new SQLiteCacheGraphDataBaseService();
                userInterfaceManager = new UserInterfaceManager(graphDataBaseManger, "Welcome to Visual Graph");

                progressManager = new SimpleProgressManager();
                threadPoolManager = Executors.newSingleThreadExecutor();

                graphLayoutManager = new GraphLayoutManager();
                graphViewManager = new GraphViewManager();

                // update helper
                updateHelper();
            } catch (Throwable ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    public static void updateHelper() {
        synchronized (generalMutex) {
            FrameworkMainService.updateHelper();

            VGMainServiceHelper.threadPoolManager = threadPoolManager;
            VGMainServiceHelper.progressManager = progressManager;
            VGMainServiceHelper.libService = libService;

            VGMainServiceHelper.graphViewManager = graphViewManager;
            VGMainServiceHelper.graphDataBaseManager = graphDataBaseManger;
            VGMainServiceHelper.graphLayoutManager = graphLayoutManager;

            VGMainServiceHelper.userInterfaceManager = userInterfaceManager;
        }
    }
}
